package com.baosight.risk.common.quartz;

import com.baosight.risk.api.riri.IRiriRiskService;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.quartz.SysJobParamMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.service.quartz.entity.SysJobParam;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * @author cuishihao
 * <p>
 * 风险自评审核
 */
@Slf4j
@Component("riskRiRiTask")
public class RiskRiRiTask {
    @Autowired
    private IRiriRiskService ririRiskService;

    @Autowired
    private XtzyCompanyMapper companyMapper;

    @Autowired
    private SysJobParamMapper sysJobParamMapper;

    public void testParams(String params, String jobId) throws Exception {
        log.error("进入定时任务————风险自评");
        String acctYear = "";
        String quarterCurrent = "";
        String manageCompCode = "";
        String manageCompName = "";
        if (StringUtils.isNotEmpty(params) && params.indexOf(",") > 1) {
            String[] split = params.split(",");
            acctYear = split[0];
            quarterCurrent = split[1];
            manageCompCode = split[2];
            manageCompName = split[3];
        }
        List<XtzyCompany> companyList=new ArrayList<XtzyCompany>();
        if(StringUtils.isNotEmpty(manageCompCode) && "BGTA".equals(manageCompCode)){
            XtzyCompany companyParam=new XtzyCompany();
            companyParam.setPathLevel((short)2);
            companyList = companyMapper.getRiskCompany(companyParam);
        }
        if(StringUtils.isEmpty(manageCompCode)){
            SysJobParam param=new SysJobParam();
            param.setJobGuid(jobId);
            List<SysJobParam> jobParamList= sysJobParamMapper.select(param);
            if (StringUtils.isEmpty(manageCompCode) && jobParamList.size()>0) {
                manageCompCode=jobParamList.get(0).getParamValue();
            }
            if (StringUtils.isEmpty(manageCompName)) {
                manageCompName = jobParamList.get(0).getParamValueDesc();
            }
        }
        if (CollectionUtils.isEmpty(companyList)) {
            throw new Exception("没有参加风险自评的公司！");
        }
        if (StringUtils.isEmpty(acctYear)) {
            acctYear = DateUtils.format(new Date(), "yyyy");
        }
        if (StringUtils.isEmpty(quarterCurrent)) {
            quarterCurrent = DateUtils.getQuarter(new Date());
        }
        Map paramMap=new HashMap();
        paramMap.put("acctYear",acctYear);
        paramMap.put("quarterCurrent",quarterCurrent);
        paramMap.put("manageCompCode",manageCompCode);
        paramMap.put("manageCompName",manageCompName);
        ririRiskService.saveTimingTask(paramMap, companyList,jobId);
        log.error("执行结束————风险自评");
    }

    public void testNoParams() {
        log.error("执行无参方法");
    }
}
